﻿//系统管理-->用户管理
//Create By:Cui 2014年9月19日12:09:48

//1.0 加载JqueryDatabase
jQuery(function ($) {
    var oTable1 = $('#sample-table-2').dataTable({
        "aoColumns": [
          { "bSortable": false },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": false },
        ],
        oLanguage: {
            "sProcessing": "正在加载中......",
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "对不起，查询不到相关数据！",
            "sEmptyTable": "表中无数据存在！",
            "sInfo": "当前显示： _START_ 到 _END_ 条，共 _TOTAL_ 条",
            "sInfoFiltered": "数据表中共为 _MAX_ 条记录",
            "sSearch": "",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "末页"
            }
        },
        iDisplayLength: 100,
        bLengthChange: false
    });


    $('table th input:checkbox').on('click', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
        .each(function () {
            this.checked = that.checked;
            $(this).closest('tr').toggleClass('selected');
        });

    });


    $('[data-rel="tooltip"]').tooltip({ placement: tooltip_placement });
    function tooltip_placement(context, source) {
        var $source = $(source);
        var $parent = $source.closest('table')
        var off1 = $parent.offset();
        var w1 = $parent.width();

        var off2 = $source.offset();
        var w2 = $source.width();

        if (parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
        return 'left';
    }
})

window.onload = function () {

    //2.0 页面初始化
    //2.1 给table加按钮（增加，删除，刷新）
    var $aa = $("#sample-table-2_wrapper .col-sm-6").eq(0);
    $aa.html("<button class='btn btn-primary btn-sm' id='butadd'   ><i class='icon-plus-sign bigger-110'></i><span class='bigger-110 no-text-shadow'>新增</span></button>");
    $aa.append("<button class='btn btn-danger btn-sm' style='margin-left: 4px' id='butdel'><i class='icon-trash bigger-110'></i><span class='bigger-110 no-text-shadow'>删除</span></button>");
    $aa.append("<button class='btn btn-success btn-sm' style='margin-left: 4px' id='buflush'><i class='icon-refresh bigger-110'></i><span class='bigger-110 no-text-shadow'>刷新</span></button>");
    $("td").addClass("tdBorder");


    //2.2 table表头上的样式
    $("#sample-table-2>thead tr>th").attr("style", "font-weight:bold;color:black;text-align:center");


    //2.3 table搜索样式
    $("#sample-table-2_filter label input").attr("placeholder", "搜索(多条件搜索中间用空格隔开)").attr("style", "width:50%");
    $("#sample-table-2_filter label").attr("style", "width:100%");

    //2.4 增加表单行间距样式
    $("div .modal-body .row:not(#iconr)").attr("style", "margin-top:7px;");
    $("div .modal-body .row .col-sm-3").attr("style", "margin-top:6px;text-align:right");
    $("div#myModal .modal-body .row i:lt(6)").attr("style", "margin-top:9px;text-align:right;");

    $("#form_field_select_4_chosen").attr("style", "width:100%");

    //3.0   功能
    //3.1   删除
    $("#butdel").click(function delCheck() {
        var idString = "";
        $("table tbody input[type='checkbox']:checked").parent().parent().next().each(function () {
            idString += $(this).text() + ","
        });
        idString = idString.substring(0, idString.length - 1);
        if (idString.length == 0) {
            $.gritter.add({
                title: '消息提醒',
                text: '请选择要删除的项',
                class_name: 'gritter-info gritter-center'
            });
        }
        else {
            if (!confirm("确定删除")) { return; }
            $.ajax({
                url: "ToDelete",
                dataType: "Json",
                data: { idString: idString },
                Type: "Post",
                success: function (data) {
                    var json = eval(data);
                    if (json.result == "Success") {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                        history.go(0);
                    }
                    else {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                    }
                }
            });
        }
    });

    //3.2   刷新
    $("#buflush").click(function () {
        history.go(0);
    });
    //遮罩层div
    $("#bgdiv").attr("style", "display:none");
}